package com.happy.zebra.admin.ui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.happy.zebra.admin.ui.entity.OrgRecommendOrg;
import com.happy.zebra.admin.ui.entity.OrgSettledCount;
import com.happy.zebra.admin.ui.mapper.OrgOrganWebsiteApplyMapper;
import com.happy.zebra.admin.ui.mapper.OrgRecommendOrgMapper;
import com.happy.zebra.admin.ui.service.OrgOrganWebsiteApplyService;
import com.happy.zebra.admin.ui.service.OrgSettledCountService;
import com.happy.zebra.admin.ui.service.impl.OrgRecommendOrgService;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.opter.api.dto.BelongDto;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/org")
public class OrgController {

	private final OrgOrganWebsiteApplyMapper applyMapper;
	/** 机构 推荐 机构 service */
	private final OrgRecommendOrgService recommendOrgService;
	/** 机构 推荐 机构 mapper */
	private final OrgRecommendOrgMapper recommendOrgMapper;
	/** 结算订单 */
	private final OrgSettledCountService settledCountService;

	@GetMapping("getWebsiteOrgApplyList")
	public R getWebsiteOrgApplyList(Integer current, Integer size, String phone) {
		return new R(applyMapper.getWebsiteOrgApplyList(new Page(current, size), phone));
	}

	@PostMapping("recommendOrg")
	public R recommendOrg(@RequestBody BelongDto dto) {

		if (dto.getIds().size() == 0) {
			return new R<>(recommendOrgService.remove(new QueryWrapper<OrgRecommendOrg>().eq("org_id", dto.getTargetId())));
		} else {
			//先解除掉所有的推荐关系
			recommendOrgService.remove(new QueryWrapper<OrgRecommendOrg>().eq("org_id", dto.getTargetId()));
			List<OrgRecommendOrg> orgRecommendOrgs = new ArrayList<>();
			//新增
			for (Integer id : dto.getIds()) {

				if (id.equals(dto.getTargetId())) {
					continue;
				}

				OrgRecommendOrg oro = new OrgRecommendOrg();
				oro.setCreateTime(LocalDateTime.now());
				oro.setOrgId(dto.getTargetId());
				oro.setRecommendOrgId(id);
				orgRecommendOrgs.add(oro);
			}
			return new R(recommendOrgService.saveBatch(orgRecommendOrgs));
		}

	}

	@GetMapping("getOrgSettledCountList")
	public R getOrgSettledCountList(Integer current, Integer size, Integer optId, String type) {
		return new R(settledCountService.getOrgSettledCountList(new Page<>(current, size), optId, type));
	}

	/**
	 * 获取未推荐合作商列表
	 *
	 * @return {@link R}
	 * @author nianhua.jiang
	 * @date 2022/1/10 11:07
	 */
	@GetMapping("getNoBelongRecommendOrgList")
	public R getNoBelongRecommendOrgList(String name) {
		return new R(recommendOrgMapper.getNoBelongRecommendOrgList(name));
	}

	/**
	 * 获取推荐合作商列表
	 *
	 * @return {@link R}
	 * @author nianhua.jiang
	 * @date 2022/1/10 11:07
	 */
	@GetMapping("getBelongRecommendOrgList")
	public R getBelongRecommendOrgList(Integer orgId) {
		return new R(recommendOrgMapper.getBelongRecommendOrgList(orgId));
	}

}
